﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_ViewTestReport : System.Web.UI.Page
{
    DataSet dsTest = new DataSet();
    static object userId;
    public static int testId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            userId = Session["UserId"];
            testId = Convert.ToInt32(Request.QueryString["TestId"]);
            dsTest = AdminCreateTestDAL.GetTestInfoByTestId(testId);
            dsTest.Tables[0].Columns.Add("TopicName", typeof(System.String));
            setValues();
        }
    }

    private void setValues()
    {
        int cntCompleted = 0, cntPending = 0;
        lablTestName.Text = dsTest.Tables[0].Rows[0][1].ToString();
        lablSubject.Text = dsTest.Tables[0].Rows[0][2].ToString();
        bindTopics();
        lablTopic.Text = dsTest.Tables[0].Rows[0]["TopicName"].ToString();
        lablNoOfQuestion.Text = dsTest.Tables[0].Rows[0][3].ToString();
        lablCutOffMarks.Text = dsTest.Tables[0].Rows[0][4].ToString();
        LablNoOfStudents.Text = dsTest.Tables[2].Rows.Count.ToString();
        for (int i = 0; i < dsTest.Tables[2].Rows.Count; i++)
        {
            if (dsTest.Tables[2].Rows[i][2].ToString().Trim() == "Completed")
                cntCompleted++;
        }
        cntPending = dsTest.Tables[2].Rows.Count - cntCompleted;
        lablCompletedBy.Text = cntCompleted.ToString();
        lablPendingBy.Text = cntPending.ToString();

        gvTest.Columns[0].Visible = true;
        gvTest.Columns[2].Visible = true;
        gvTest.Columns[3].Visible = true;
        gvTest.Columns[4].Visible = true;

        DataSet dsInfo = new DataSet();
        dsInfo = AdminCreateTestDAL.GetTestAttemptFullInfo(testId);
        lablCompletedCount.Text = dsInfo.Tables[0].Rows[0][0].ToString();
        lablNotCompletedCount.Text = dsInfo.Tables[1].Rows[0][0].ToString();
        lablNotAttemptedCount.Text = dsInfo.Tables[2].Rows[0][0].ToString();

        DataTable dt = new DataTable();
        dt = AdminCreateTestDAL.GetTestAttemptAllInfoWithUserInfo(userId, testId, "Completed");
        gvTest.DataSource = dt;
        gvTest.DataBind();
    }

    //private void bindGrid()
    //{
    //    DataTable dt = new DataTable();
    //    dt = AdminCreateTestDAL.GetTestAttemptAllInfoWithUserInfo(userId, testId);
    //    gvTest.DataSource = dt;
    //    gvTest.DataBind();
    //}

    private void bindTopics()
    {
        int i, j;
        string topics;

        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            for (j = 0; j < dsTest.Tables[1].Rows.Count; j++)
            {
                if (dsTest.Tables[0].Rows[i][0].ToString() == dsTest.Tables[1].Rows[j][0].ToString())
                    dsTest.Tables[0].Rows[i]["TopicName"] += dsTest.Tables[1].Rows[j][1] + ",";
            }
        }

        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            topics = dsTest.Tables[0].Rows[i]["TopicName"].ToString();
            topics = topics.Substring(0, topics.Length - 1);
            dsTest.Tables[0].Rows[i]["TopicName"] = topics;
        }
    }

    protected void gvAssignment_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int attemptId = Convert.ToInt32(e.CommandArgument);

        switch (e.CommandName)
        {
            case "btnScoreClick": Response.Redirect("../Student/Score.aspx?AttemptId=" + attemptId);
                break;

            case "btnLinkPerformanceClick":
                DataTable dtuser = new DataTable();
                dtuser = AdminCreateTestDAL.GetUserIdBasedOnAttemptId(attemptId);
                Response.Redirect("../Student/ViewPerformanceUser.aspx?UserId=" + dtuser.Rows[0][0].ToString());
                break;
        }  

    }
    protected void lnkTestCompleted_Click(object sender, EventArgs e)
    {
        gvTest.Columns[0].Visible = true;
        gvTest.Columns[2].Visible = true;
        gvTest.Columns[3].Visible = true;
        gvTest.Columns[4].Visible = true;

        DataTable dt = new DataTable();
        dt = AdminCreateTestDAL.GetTestAttemptAllInfoWithUserInfo(userId, testId,"Completed");
        gvTest.DataSource = dt;
        gvTest.DataBind();

    }
    protected void lnkTestNotCompleted_Click(object sender, EventArgs e)
    {
        gvTest.Columns[0].Visible = true;
        gvTest.Columns[2].Visible = true;
        gvTest.Columns[3].Visible = true;
        gvTest.Columns[4].Visible = true;

        DataTable dt = new DataTable();
        dt = AdminCreateTestDAL.GetTestAttemptAllInfoWithUserInfo(userId, testId,"Not Completed");
        gvTest.DataSource = dt;
        gvTest.DataBind();
    }
    protected void lnkTestNotAttempted_Click(object sender, EventArgs e)
    {
        gvTest.Columns[0].Visible = false;
        gvTest.Columns[2].Visible = false;
        gvTest.Columns[3].Visible = false;
        gvTest.Columns[4].Visible = false;
        gvTest.Columns[5].Visible = false;

        DataTable dt = new DataTable();
        dt = AdminCreateTestDAL.GetTestAttemptAllInfoWithUserInfo(userId, testId,"Not Attempted");
        gvTest.DataSource = dt;
        gvTest.DataBind();

    }
}